﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using Dapper;
using ExaminationSys.DataAccess.Entity;

namespace ExaminationSys.DataAccess.Repository
{
    public class Sys_UserRepository : Repository<Sys_UserEntity>
    {
        public Sys_UserRepository(SqlConnection context)
            : base(context)
        {
        }
        private SqlConnection db;
        public override Sys_UserEntity GetById(dynamic id)
        {
            return base.ObjectSet.Get<Sys_UserEntity>(db, id, null, null);
        }
    }

    public class Sys_UserService
    {
        private Sys_UserService()
        {
        }

        public Sys_UserEntity Get(Guid id)
        {
            using (var db = DataContent.DBServer())
            {
                IUnitOfWork work = new UnitOfWork(db);
                var result = work.Sys_UserEntity.GetById(id);
                return result;
            }
        }

        public List<Sys_UserEntity> GetAll()
        {
            using (var db = DataContent.DBServer())
            {
                IUnitOfWork work = new UnitOfWork(db);
                var result = work.Sys_UserEntity.GetList();
                return result.ToList();
            }
        }
        public bool Update(Sys_UserEntity entity)
        {
            using (var db = DataContent.DBServer())
            {
                IUnitOfWork work = new UnitOfWork(db);
                return work.Sys_UserEntity.Update(entity);
            }
        }
        public dynamic Insert(Sys_UserEntity entity)
        {
            using (var db = DataContent.DBServer())
            {
                IUnitOfWork work = new UnitOfWork(db);
                return work.Sys_UserEntity.Insert(entity, null);
            }
        }

        public List<Sys_UserEntity> GetListByPage(string sqlwhere, string orderbyFiled, int pagesize, int pageindex, out int totalPage, out int totalPageCount)
        {
            if (string.IsNullOrWhiteSpace(orderbyFiled))
                throw new ArgumentNullException("orderbyFiled");

            using (var db = DataContent.DBServer())
            {
                var obj = new
                {
                    TableName = "Sys_User",
                    Fields = "*",
                    OrderField = orderbyFiled,
                    sqlWhere = sqlwhere,
                    pageSize = pagesize,
                    pageIndex = pageindex,
                };

                var dynamicParam = new DynamicParameters(obj);
                dynamicParam.Add("TotalPage", 0, DbType.Int32, ParameterDirection.Output, 32);
                dynamicParam.Add("TotalRecord", 0, DbType.Int32, ParameterDirection.Output, 32);
                var items = db.Query<Sys_UserEntity>("PagingRecord", dynamicParam, null, true, null, CommandType.StoredProcedure);
                totalPage = dynamicParam.Get<int>("TotalPage");
                totalPageCount = dynamicParam.Get<int>("TotalRecord");
                return items.ToList();
            }
        }
    }
}
